<template>
  <div class="app-container">
    <div class="filter-container">
      <el-form v-model="listQuery" inline>
        <el-form-item label="建筑">
          <el-cascader
            v-model="placeName"
            :options="options"
            size="small"
            @change="getplace"
          ></el-cascader>
        </el-form-item>
        <el-form-item label="">
          <el-select
            v-model="listQuery.name"
            size="small"
            default-first-option
            style="width: 200px"
            placeholder="能源分项"
            clearable
            @change="change"
          >
            <el-option
              v-for="(item, index) in joinArr"
              :key="index"
              :label="item.name"
              :value="item.name"
            />
          </el-select>
        </el-form-item>
        <el-form-item>
          <el-date-picker
            v-model="value1"
            size="small"
            type="date"
            placeholder="选择日期"
          >
          </el-date-picker>
        </el-form-item>

        <el-form-item>
          <div style="display:inline">
            <el-radio-group
              v-model="radio"
              size="small"
              @change="changeDateType"
            >
              <el-radio-button label="日"></el-radio-button>
              <el-radio-button label="月"></el-radio-button>
              <el-radio-button label="年"></el-radio-button>
            </el-radio-group>
          </div>
        </el-form-item>
        <el-form-item>
          <el-button
            size="small"
            class="filter-item"
            type="primary"
            icon="el-icon-search"
            @click="handleFilter"
          >
            查询
          </el-button>
          <el-button
            size="small"
            class="filter-item"
            type="primary"
            icon="edit"
          >
            导出
          </el-button>
        </el-form-item>
      </el-form>
    </div>
    <!-- <div class="table-container">
      <el-table
        key="tableKey"
        v-loading="listLoading"
        :data="list"
        element-loading-text="加载中"
        border
        fit
        stripe
        highlight-current-row
        style="width: 100%"
        height="300"
        :header-cell-style="{
          background: '#E3ECFB',
          color: '#303133',
          fontWeight: '500'
        }"
      >
        <el-table-column align="center" label="编号" width="100">
          <template slot-scope="scope">
            <span>{{ scope.row.id }}</span>
          </template>
        </el-table-column>
        <el-table-column align="center" label="能源" width="200">
          <template slot-scope="scope">
            <span>{{ scope.row.name }}</span>
          </template>
        </el-table-column>
        <el-table-column label="尖" align="center">
          <el-table-column
            align="center"
            label="站点图片"
            show-overflow-tooltip
          >
            <template slot-scope="scope">
              <el-image
                style="width: 60px;"
                :src="scope.row.image_path"
                :preview-src-list="[scope.row.image_path]"
              />
            </template>
          </el-table-column>
          <el-table-column align="center" label="电">
            <template slot-scope="scope">
              <span>{{ scope.row.title }}</span>
            </template>
          </el-table-column>

          <el-table-column align="center" label="水">
            <template slot-scope="scope">
              <span>{{
                scope.row.link_url == null ? "无" : scope.row.link_url
              }}</span>
            </template>
          </el-table-column>

          <el-table-column align="center" label="气">
            <template slot-scope="scope">
              <span>{{
                scope.row.status == null ? "无" : scope.row.status
              }}</span>

            </template>
          </el-table-column>
          <el-table-column align="center" label="热">
            <template slot-scope="scope">
              <span>{{
                scope.row.link_url == null ? "无" : scope.row.link_url
              }}</span>
            </template>
          </el-table-column>
        </el-table-column>
        <el-table-column label="峰" align="center">
          <el-table-column align="center" label="水">
            <template slot-scope="scope">
              <span>{{
                scope.row.link_url == null ? "无" : scope.row.link_url
              }}</span>
            </template>
          </el-table-column>
          <el-table-column align="center" label="电">
            <template slot-scope="scope">
              <span>{{
                scope.row.link_url == null ? "无" : scope.row.link_url
              }}</span>
            </template>
          </el-table-column>
          <el-table-column align="center" label="气">
            <template slot-scope="scope">
              <span>{{
                scope.row.link_url == null ? "无" : scope.row.link_url
              }}</span>
            </template>
          </el-table-column>
          <el-table-column align="center" label="热">
            <template slot-scope="scope">
              <span>{{
                scope.row.link_url == null ? "无" : scope.row.link_url
              }}</span>
            </template>
          </el-table-column>
        </el-table-column>
        <el-table-column label="能耗数据" align="center">
          <el-table-column
            align="center"
            label="站点用电量"
            class-name="overflow-on"
            width="170"
          >
            <template slot-scope="scope">
              {{ scope.row.sort }}
              <el-input
                v-if="scope.row.show == 1"
                v-model="scope.row.sort"
                size="small"
                max="50"
              />
            </template>
          </el-table-column>
          <el-table-column align="center" label="数据上报时间" width="170">
            <template slot-scope="scope">
              <span>{{ scope.row.updated_at }}</span>
            </template>
          </el-table-column>
        </el-table-column>

      </el-table>
      <div class="pagination-container text-right">
        <el-pagination
          background
          :total="pagination.total"
          :current-page="listQuery.page"
          :page-size="listQuery.limit"
          layout="total, sizes, prev, pager, next, jumper"
          @size-change="handleSizeChange"
          @current-change="handleCurrentChange"
        />
      </div>
    </div> -->
    <div class="box-container">
      <div class="chart-box all">
        <div class="title-content">能源趋势</div>
        <div class="item-content">
          <chart
            ref="chartOneRef"
            v-if="chartOneData"
            :option="chartOneOption"
            style="width: 95%;"
          />
          <!-- <i class="el-icon-download" style="color: #fff;" /> -->
        </div>
      </div>
      <!-- <div class="chart-box">
        <div class="title-content">能源趋势</div>
        <div class="item-content">
          <chart
            v-if="chartThreeData"
            :option="chartThreeOption"
            style="width: 95%;"
          />
        </div>
      </div>
      <div class="chart-box">
        <div class="title-content">年逐日能耗柱图</div>
        <div class="item-content">
          <chart
            v-if="chartFourData"
            :option="chartFourOption"
            style="width: 95%;"
          />
        </div>
      </div>
      <div class="chart-box half">
        <div class="title-content">能源排名</div>
        <div class="item-content">
          <chart
            v-if="chartTwoData"
            :option="chartTwoOption"
            style="width: 95%;"
          />
        </div>
      </div>
      <div class="chart-box half">
        <div class="title-content">能源占比</div>
        <div class="item-content">
          <chart
            v-if="chartTwoData"
            :option="chartTwoOption"
            style="width: 95%;"
          />
        </div>
      </div> -->
    </div>

    <el-dialog :title="dialogTitle" :visible.sync="dialogFormVisible">
      <el-form
        class="small-space"
        :model="temp"
        label-position="right"
        label-width="100px"
      >
        <el-form-item label="位置" required>
          <el-select
            v-model="temp.category_id"
            size="small"
            default-first-option
            style="width: 180px"
            placeholder="所属位置"
            clearable
          >
            <el-option
              v-for="(item, index) in joinArr"
              :key="index"
              :label="item.name"
              :value="item.category_id"
            />
          </el-select>
        </el-form-item>
        <el-form-item label="名称" required>
          <el-input v-model="temp.title" size="small" width="100px" />
        </el-form-item>

        <el-form-item label="跳转URL">
          <el-input
            v-model="temp.link_url"
            size="small"
            width="300"
            placeholder="例如：https://www.baidu.com"
          />
        </el-form-item>

        <el-form-item label="banner图" required>
          <el-upload
            class="avatar-uploader"
            :action="uploadPath"
            :headers="header"
            :show-file-list="false"
            :on-success="handleImageSuccess"
          >
            <img
              v-if="temp.image_path"
              :src="temp.image_path"
              style="width: 200px"
            />
            <i v-else class="el-icon-plus avatar-uploader-icon" />
          </el-upload>
        </el-form-item>

        <el-form-item label="排序" prop="sort">
          <el-input-number v-model="temp.sort" size="small" />
          <el-tag type="warning">
            说明：正序排列，前台页面显示顺序由小到大显示</el-tag
          >
        </el-form-item>
        <el-alert
          :closable="false"
          title="温馨提示：图片尽量压缩后上传，尽量不超过1M"
          type="warning"
        />
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="dialogFormVisible = false">取 消</el-button>
        <el-button
          v-if="dialogStatus == 'create'"
          type="primary"
          @click="create"
          >确 定</el-button
        >
        <el-button v-else type="primary" @click="update">确 定</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import {
  List,
  Save,
  Del,
  Status,
  Sort,
  Ct_List,
  Ct_Save,
  Ct_Del,
  Ct_Status,
  Ct_Sort
} from "@/api/banner";
import { getToken } from "@/utils/auth";
import { uploadPath } from "@/utils/common";
import chart from "@/components/Echarts/chart";
const colors = [
  "#5470c6",
  "#91cc75",
  "#fac858",
  "#ee6666",
  "#73c0de",
  "#3ba272",
  "#fc8452",
  "#9a60b4",
  "#ea7ccc"
];

export default {
  filters: {
    statusFilter(status) {
      const statusMap = {
        published: "success",
        draft: "gray",
        deleted: "danger"
      };
      return statusMap[status];
    }
  },
  components: {
    chart
  },
  data() {
    return {
      list: null,
      joinArr: [
        { name: "空调用电" },
        { name: "动力用电" },
        { name: "照明用电" },
        { name: "其他" }
      ],
      radio: "日",
      changeIndex: -1,
      pagination: {},
      dialogFormVisible: false,
      temp: {
        title: "",
        link_url: "",
        image_path: "",
        sort: 0
      },
      listQuery: {
        page: 1,
        limit: 10,
        title: "",
        name: null
      },
      uploadPath: uploadPath,
      dialogStatus: null,
      dialogTitle: "",
      header: {
        Authorization: getToken()
      },
      chartOne: {},
      chartThree: {},
      chartOneOption: {},
      chartTwoOption: {},
      chartThreeOption: {},
      chartFourOption: {},
      chartOneData: false,
      chartTwoData: false,
      chartThreeData: false,
      chartFourData: false,
      placeName: ["1", "11", "111"],
      options: [
        {
          value: "1",
          label: "办公建筑",
          children: [
            {
              value: "11",
              label: "办公建筑E楼",
              children: [
                {
                  value: "111",
                  label: "一楼"
                },
                {
                  value: "112",
                  label: "二楼"
                },
                {
                  value: "113",
                  label: "三楼"
                },
                {
                  value: "114",
                  label: "四楼"
                }
              ]
            },
            {
              value: "12",
              label: "办公建筑F楼",
              children: [
                {
                  value: "121",
                  label: "一楼"
                },
                {
                  value: "122",
                  label: "二楼"
                },
                {
                  value: "123",
                  label: "三楼"
                },
                {
                  value: "124",
                  label: "四楼"
                }
              ]
            }
          ]
        }
      ],
      date: [
        "0",
        "1",
        "2",
        "3",
        "4",
        "5",
        "6",
        "7",
        "8",
        "9",
        "10",
        "11",
        "12",
        "13",
        "14",
        "15",
        "16",
        "17",
        "18",
        "19",
        "20",
        "21",
        "22",
        "23"
      ]
    };
  },
  created() {
    this.getList();
    // this.getCtList(),
    this.getLineCount();
    this.getPieCount();
    this.getLineTwo();
    this.getBar();
  },

  methods: {
    getplace() {
      console.log(this.placeName);
    },
    getList() {
      this.listLoading = true;
      List(this.listQuery).then(response => {
        var data = response.data;
        this.list = data.data;
        this.pagination = data;
        // 图片相对路径处理
        // this.list.forEach(item=>{
        //  item.image_path=process.env.VUE_APP_BASE_PIC +  item.image_path

        // })

        // 图片放大处理，声明一个数组，遍历一遍
        // this.list.forEach(e => {
        //   e.imglist=[]
        //   e.imglist.push(e.image_path)
        // });
      });
      this.listLoading = false;
    },
    changeDateType(val) {
      this.chartOneData = false;
      if (val == "日") {
        this.date = [
          "0",
          "1",
          "2",
          "3",
          "4",
          "5",
          "6",
          "7",
          "8",
          "9",
          "10",
          "11",
          "12",
          "13",
          "14",
          "15",
          "16",
          "17",
          "18",
          "19",
          "20",
          "21",
          "22",
          "23"
        ];
      } else if (val == "月") {
        this.date = [
          "1",
          "2",
          "3",
          "4",
          "5",
          "6",
          "7",
          "8",
          "9",
          "10",
          "11",
          "12",
          "13",
          "14",
          "15",
          "16",
          "17",
          "18",
          "19",
          "20",
          "21",
          "22",
          "23",
          "24",
          "25",
          "26",
          "27",
          "28",
          "29",
          "30"
        ];
      } else if (val == "年") {
        this.date = [
          "1",
          "2",
          "3",
          "4",
          "5",
          "6",
          "7",
          "8",
          "9",
          "10",
          "11",
          "12"
        ];
      }

      this.$nextTick(() => {
        this.getLineCount();
        if (this.$refs.chartOneRef) {
          this.$refs.chartOneRef.initChart();
        }
      });
    },
    handleFilter() {
      this.listQuery.page = 1;
      this.getList();
    },
    handleSizeChange(val) {
      this.listQuery.limit = val;
      this.getList();
    },

    handleCurrentChange(val) {
      this.listQuery.page = val;
      this.getList();
    },
    change(val) {
      this.$set(this.listQuery, "category_idC", null);

      List({ category_id: val }).then(response => {
        var data = response.data;
      });
    },
    showCreate() {
      this.temp = {
        category_id: null,
        title: "",
        link_url: "",
        image_path: "",
        sort: 0
      };
      this.dialogTitle = "添加";
      this.dialogStatus = "create";
      (this.dialogTitle = "添加"), (this.dialogFormVisible = true);
    },
    handleImageSuccess(res, file) {
      if (res.code === 0) {
        this.temp.image_path = res.data.filepath;
        // 图片相对路径处理
        // this.temp.image_path=process.env.VUE_APP_BASE_PIC +  this.temp.image_path
        this.temp.image_path = this.temp.image_path;
        // uploadPath = uploadPath.this.temp.image_path;

        this.$message.success("上传成功！");
      } else {
        this.$message.error(res.msg);
      }
    },
    update() {
      Save(this.temp).then(response => {
        if (response.code === 0) {
          this.$message({
            message: response.msg,
            type: "success"
          });
          this.dialogFormVisible = false;
          // this.fetchSetting()
          this.getList();
        } else {
          this.$message({
            message: response.msg,
            type: "error"
          });
        }
      });
    },
    // 绘制图表1
    getLineCount() {
      var SCCounts = {
        // title: {
        //   text: '本月访问趋势',
        //   x: 'center'
        //   // y: '35%',
        //   // itemGap: 2, // 副标题和主标题之间的距离，默认是10
        // },
        // color: colorList,
        tooltip: {
          trigger: "item",
          formatter: "{a} <br/>{b} : {c}"
        },
        legend: {
          data: ["水", "电"],
          textStyle: {
            color: "#000"
          }
        },
        textStyle: {
          color: "#000"
        },
        grid: {
          left: 50,
          right: 50,
          top: 30,
          bottom: 30,
          borderWidth: 1
        },
        xAxis: {
          name: "时间",
          type: "category",
          boundaryGap: true,
          data: this.date
        },
        yAxis: {
          name: "单位：kW",
          type: "value",
          nameTextStyle: {
            color: "#000"
          }
        },
        series: [
          // {
          //   name: "水",
          //   data: [820, 932, 901, 934, 1290, 1330, 1320],
          //   type: "line",
          //   smooth: true,
          //   symbolSize: 8
          //   // itemStyle: {
          //   //   color: 'rgb(140,205,130)'
          //   // }
          // },
          {
            name: "电",
            data: [
              1820,
              1932,
              1901,
              1934,
              1890,
              2330,
              2320,
              6690,
              6890,
              7890,
              9899,
              12000,
              12322,
              11202,
              11002,
              10202,
              9202,
              11002,
              9222,
              8890,
              6578,
              4569,
              2390,
              2290
            ],
            type: "line",
            smooth: true,
            symbolSize: 8
            // itemStyle: {
            //   color: 'rgb(140,205,130)'
            // }
          }
        ]
      };
      this.chartOneOption = SCCounts;
      this.chartOneData = true;

      this.chartThreeOption = SCCounts;
      this.chartThreeData = true;

      // var myChart = this.$echarts.init(this.$refs.SUMEchart)
      // SCCounts && myChart.setOption(SCCounts)
    },
    // 绘制饼图
    getPieCount() {
      var SCCounts = {
        // title: {
        //   text: '本月访问趋势',
        //   x: 'center'
        //   // y: '35%',
        //   // itemGap: 2, // 副标题和主标题之间的距离，默认是10
        // },
        // color: colorList,
        tooltip: {
          trigger: "item"
        },
        legend: {
          left: "center",
          textStyle: {
            color: "#000"
          }
        },
        series: [
          {
            name: "能耗",
            type: "pie",
            top: "10%",
            radius: ["30%", "60%"],
            avoidLabelOverlap: false,
            label: {
              show: true,
              // position: 'center',
              color: "#000"
            },
            labelLine: {
              show: true
            },
            data: [
              { value: 1048, name: "水" },
              { value: 735, name: "电" },
              { value: 580, name: "气" },
              { value: 484, name: "热" }
            ],
            emphasis: {
              itemStyle: {
                shadowBlur: 10,
                shadowOffsetX: 0,
                shadowColor: "rgba(0, 0, 0, 0.5)"
              }
            }
          }
        ]
      };
      this.chartTwoOption = SCCounts;
      this.chartTwoData = true;
    },
    // 绘制对比图
    getLineTwo() {
      var SCCounts = {
        color: colors,
        tooltip: {
          trigger: "none",
          axisPointer: {
            type: "cross"
          }
        },
        legend: {
          textStyle: {
            color: "#000"
          }
        },
        grid: {
          top: 70,
          bottom: 50
        },
        textStyle: {
          color: "#000"
        },
        xAxis: [
          {
            type: "category",
            axisTick: {
              alignWithLabel: true
            },
            axisLine: {
              onZero: false,
              lineStyle: {
                color: colors[1]
              }
            },
            axisPointer: {
              label: {
                formatter: function(params) {
                  return (
                    "Precipitation  " +
                    params.value +
                    (params.seriesData.length
                      ? "：" + params.seriesData[0].data
                      : "")
                  );
                }
              }
            },
            yAxisIndex: 1,
            // prettier-ignore
            data: ['2016-1', '2016-2', '2016-3', '2016-4', '2016-5', '2016-6', '2016-7', '2016-8', '2016-9', '2016-10', '2016-11', '2016-12']
          },
          {
            type: "category",
            axisTick: {
              alignWithLabel: true
            },
            axisLine: {
              onZero: false,
              lineStyle: {
                color: colors[0]
              }
            },
            axisPointer: {
              label: {
                formatter: function(params) {
                  return (
                    "Precipitation  " +
                    params.value +
                    (params.seriesData.length
                      ? "：" + params.seriesData[0].data
                      : "")
                  );
                }
              }
            },
            yAxisIndex: 2,
            // prettier-ignore
            data: ['2015-1', '2015-2', '2015-3', '2015-4', '2015-5', '2015-6', '2015-7', '2015-8', '2015-9', '2015-10', '2015-11', '2015-12']
          }
        ],
        yAxis: [
          {
            type: "value",
            axisLine: {
              show: true
            }
          },
          {
            type: "value",
            position: "right",
            alignTicks: true,
            axisLine: {
              show: true
            }
          }
        ],
        series: [
          {
            name: "能耗(2023)",
            type: "line",
            xAxisIndex: 1,
            smooth: true,
            emphasis: {
              focus: "series"
            },
            data: [
              2.6,
              5.9,
              9.0,
              26.4,
              28.7,
              70.7,
              175.6,
              182.2,
              48.7,
              18.8,
              6.0,
              2.3
            ]
          },
          {
            name: "能耗(2024)",
            type: "line",
            smooth: true,
            emphasis: {
              focus: "series"
            },
            data: [
              3.9,
              5.9,
              11.1,
              18.7,
              48.3,
              69.2,
              231.6,
              46.6,
              55.4,
              18.4,
              10.3,
              0.7
            ]
          }
        ]
      };
      this.chartThreeOption = SCCounts;
      this.chartThreeData = true;
    },
    getBar() {
      var SCCounts = {
        color: colors,
        tooltip: {
          trigger: "none",
          axisPointer: {
            type: "cross"
          }
        },
        legend: {
          textStyle: {
            color: "#000"
          }
        },
        textStyle: {
          color: "#000"
        },
        grid: {
          top: 70,
          bottom: 50
        },
        xAxis: [
          {
            type: "category",
            axisTick: {
              alignWithLabel: true
            },
            axisLine: {
              onZero: false,
              lineStyle: {
                color: colors[1]
              }
            },
            axisPointer: {
              label: {
                formatter: function(params) {
                  return (
                    "Precipitation  " +
                    params.value +
                    (params.seriesData.length
                      ? "：" + params.seriesData[0].data
                      : "")
                  );
                }
              }
            },
            // prettier-ignore
            data: ['2016-1', '2016-2', '2016-3', '2016-4', '2016-5', '2016-6', '2016-7', '2016-8', '2016-9', '2016-10', '2016-11', '2016-12']
          },
          {
            type: "category",
            axisTick: {
              alignWithLabel: true
            },
            axisLine: {
              onZero: false,
              lineStyle: {
                color: colors[0]
              }
            },
            axisPointer: {
              label: {
                formatter: function(params) {
                  return (
                    "Precipitation  " +
                    params.value +
                    (params.seriesData.length
                      ? "：" + params.seriesData[0].data
                      : "")
                  );
                }
              }
            },
            // prettier-ignore
            data: ['2015-1', '2015-2', '2015-3', '2015-4', '2015-5', '2015-6', '2015-7', '2015-8', '2015-9', '2015-10', '2015-11', '2015-12']
          }
        ],
        yAxis: [
          {
            type: "value"
          }
        ],
        series: [
          {
            name: "Precipitation(2015)",
            type: "bar",
            xAxisIndex: 1,
            smooth: true,
            emphasis: {
              focus: "series"
            },
            data: [
              2.6,
              5.9,
              9.0,
              26.4,
              28.7,
              70.7,
              175.6,
              182.2,
              48.7,
              18.8,
              6.0,
              2.3
            ]
          },
          {
            name: "Precipitation(2016)",
            type: "bar",
            smooth: true,
            emphasis: {
              focus: "series"
            },
            data: [
              3.9,
              5.9,
              11.1,
              18.7,
              48.3,
              69.2,
              231.6,
              46.6,
              55.4,
              18.4,
              10.3,
              0.7
            ]
          }
        ]
      };
      this.chartFourOption = SCCounts;
      this.chartFourData = true;
    }
  }
};
</script>

<style lang="scss" scoped>
.alert-container {
  //margin:0px;
}

.box-container {
  width: 100%;
  display: flex;
  flex-wrap: wrap;
  margin: 0px 0;
  justify-content: space-between;

  .chart-box {
    width: 49.7%;
    height: 650px;
    background-color: #fff;
    margin-bottom: 10px;

    .title-content {
      display: flex;
      justify-content: space-between;
      color: #000;
      height: 50px;
      line-height: 50px;
      padding: 0 10px;
      background: #dfdfdf;
      align-items: center;
    }

    .item-content {
      display: flex;
      flex-wrap: wrap;
      height: calc(100% - 50px);
      padding: 10px;
    }
  }

  .half {
    width: 24.7%;
  }

  .all {
    width: 100%;
  }
}
</style>
